package edu.ztr.service.impl;

import edu.ztr.mapper.LoginMapper;
import edu.ztr.pojo.Key;
import edu.ztr.service.LoginServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class LoginServiceImpl implements LoginServer {

    @Autowired
    private LoginMapper loginMapper;

    // 获得验证码
    @Override
    public String getCheck() {
        Random r = new Random();
        String s = "";
        for (int i = 0; i < 4; i++) {
            int f = r.nextInt(2);
            int t = r.nextInt(26);
            if (f == 1) {
                s += (char) ('a' + t);
            } else {
                s += (char) ('A' + t);
            }
        }
        return s;
    }

    // 登录查询
    @Override
    public Key login(Key key) {
        return loginMapper.login(key);
    }

    // 注册
    @Override
    public String register(Key key) {
        if (key.getName().equals("") ) {
            return "账户不能为空";
        }
        if(key.getKeyword().equals("")){
            return "密码不能为空";
        }
        Key e = loginMapper.search(key);
        // 如果找到了
        if (e != null) {
            return "用户名已被注册";
        }
        loginMapper.register(key);
        return "OK";
    }
}
